package com.cskaoyan.demo.generic;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

//使用<？ extends xxx>实现协变


public class Demo1 {
    static Collection<Student1> collection1;
    static Collection<People> collection2;
    public static Collection m1(Collection<? extends People> c){

        return (Collection<People>) c;
    }
    public static void main(String[] args) {
//        Collection<Student1> stuList = new Collection<>();
        List<Student1> student1List = new ArrayList<>();
        student1List.add(new Student1("zs",18));

        collection2 = m1(student1List);
        collection2.add(new Student2("ls",19));

        collection2.forEach(
                o -> {
                    System.out.println("o = " + o);
                }
        );
    }





}
@Data
@NoArgsConstructor
@AllArgsConstructor
class People{
    String name;

}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Student1 extends People{
    String name1;
    int age1;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Student2 extends People{
    String name2;
    int age2;
}

